<?php
namespace App\Controllers\My;

use App\Controllers\BaseController;
use App\Library\App;
use App\Library\Helper;
use App\Models\SysUser;

class IndexController extends BaseController {

    public function initialize() {
        parent::initialize();
        if($this->request->isPost()) {
            $this->checkLogin();
        }
    }

    //---------------------------------------------------------------------------------
    //查看系统用户信息
    //---------------------------------------------------------------------------------
    public function detailAction() {
        if($this->request->isGet()) {
            $this->display('/my/detail');
        } else {
            $uid = App::getCurrentUserUid();
            if($uid !== false) {
                $user   = new SysUser();
                $entity = $user->get(intval($uid));
                unset($entity['password']);
                unset($entity['role_id']);
                return $this->sendAjax(true, $entity);
            } else {
                return $this->sendAjax(true, '系统错误，请稍候再试！');
            }
        }
    }

    //---------------------------------------------------------------------------------
    //修改系统用户密码
    //---------------------------------------------------------------------------------
    public function resetpasswordAction() {
        if($this->request->isGet()) {
            $this->display('/my/resetpassword');
        } else {
            $data = [
                'name'          => $this->request->getPost('name'),
                'old_password'  => $this->request->getPost('old_password'),
                'new_password'  => $this->request->getPost('new_password'),
                'new_password2' => $this->request->getPost('new_password2'),
            ];
            if($data['old_password'] == '') {
                return $this->sendAjax(false, '修改密码失败，旧密码不能为空！');
            }
            if($data['new_password'] == '') {
                return $this->sendAjax(false, '修改密码失败，新密码不能为空！');
            }
            if(strlen($data['new_password']) < 6) {
                return $this->sendAjax(false, '修改密码失败，新密码的长度必须大于等于6位！');
            }
            if(strlen($data['new_password']) != strlen(trim($data['new_password']))) {
                return $this->sendAjax(false, '修改密码失败，新密码两端不能出现空格！');
            }
            if($data['new_password'] != $data['new_password2']) {
                return $this->sendAjax(false, '修改密码失败，两次输入的新密码不一致！');
            }

            $uid = App::getCurrentUserUid();
            if($uid !== false) {
                $user   = new SysUser();
                $entity = $user->get($uid);
                if($entity['password'] != md5($data['old_password'])) {
                    return $this->sendAjax(false, '修改密码失败，旧密码不正确！');
                }
                $updateEntity = [
                    'id'       => $entity['id'],
                    'password' => md5($data['new_password'])
                ];
                if($user->resetPassword($updateEntity)) {
                    return $this->sendAjax(true, '修改密码成功！');
                }
                return $this->sendAjax(false, '修改密码失败，系统错误！');
            } else {
                return $this->sendAjax(true, '系统错误，请稍候再试！');
            }
        }
    }

    //---------------------------------------------------------------------------------
    //修改资料
    //---------------------------------------------------------------------------------
    public function resetprofileAction() {
        if($this->request->isGet()) {
            $this->display('/my/resetprofile');
        } else {
            $data = [
                'name'   => $this->request->getPost('name'),
                'mobile' => $this->request->getPost('mobile'),
                'tel'    => $this->request->getPost('tel'),
                'email'  => $this->request->getPost('email'),
            ];
            if($data['name'] == '') {
                return $this->sendAjax(false, '修改资料失败，姓名不能为空！');
            }
            if(mb_strlen($data['name'], 'utf-8') > 10) {
                return $this->sendAjax(false, '修改资料失败，用户名称太长,不能超过10个字！');
            }
            if(isset($data['mobile']) && $data['mobile'] != '') {
                if(!Helper::isMobile($data['mobile'])) {
                    return $this->sendAjax(false, '修改资料失败，电话号码格式不正确！');
                }
            }
            $uid = App::getCurrentUserUid();
            if($uid !== false) {
                $user         = new SysUser();
                $entity       = $user->get($uid);
                $updateEntity = [
                    'id'     => $entity['id'],
                    'name'   => $data['name'],
                    'mobile' => $data['mobile'],
                    'tel'    => $data['tel'],
                    'email'  => $data['email']
                ];
                if($user->edit($updateEntity)) {
                    return $this->sendAjax(true, '修改资料成功！');
                }
                return $this->sendAjax(false, '修改资料失败，系统错误！');
            }
            return $this->sendAjax(false, '系统错误，请稍候再试！');
        }
    }

    //---------------------------------------------------------------------------------
    //获取系统用户信息
    //---------------------------------------------------------------------------------
    public function getAction() {
        $uid = App::getCurrentUserUid();
        if($uid !== false) {
            $user   = new SysUser();
            $entity = $user->get($uid);
            return $this->sendAjax(true, $entity);
        }
        return $this->sendAjax(false, '系统错误，请稍候再试！');
    }
}
